package com.prince.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonUtil {

	private JsonUtil(){}

	public static <T> T parseObject(String json,Class<T> clazz){
		return JSON.parseObject(json, clazz);
	}

	public static <T> T parseObject(Map map,Class<T> clazz){
		String json = toJSONString(map);
		return JSON.parseObject(json, clazz);
	}
	public static String toJSONString(Object object){
		return JSON.toJSONString(object);
	}
	//将Unicode编码转换为汉字
	public static String convert(String utfString){
		StringBuilder sb = new StringBuilder();
		int i = -1;
		int pos = 0;
		
		while((i=utfString.indexOf("\\u", pos)) != -1){
			sb.append(utfString.substring(pos, i));
			if(i+5 < utfString.length()){
				pos = i+6;
				sb.append((char)Integer.parseInt(utfString.substring(i+2, i+6), 16));
			}
		}
		return sb.toString();
	}
	
	/**
	 * url截取转json
	 * @param param
	 * @return
	 */
	 public static String urlToJson(String param) {
			String str[]=param.split("&");
			String s = "{";
			for (int i = 0; i < str.length; i++) {
				String string = str[i];
				if(i==str.length-1){
					if(string.indexOf("{")>0){
						s += "\""+string.replaceFirst("=", "\":")+"";
					}else if(string.lastIndexOf("==")>0){
						s += "\""+string.replaceFirst("=", "\":\"")+"\"";
					}else{
						s += "\""+string.replace("=", "\":\"")+"\"";
					}
				}else{
					if(string.indexOf("{")>0){
						s += "\""+string.replaceFirst("=", "\":")+",";
					}else if(string.lastIndexOf("==")>0){
						s += "\""+string.replaceFirst("=", "\":\"")+"\",";
					}else{
						s += "\""+string.replace("=", "\":\"")+"\",";
					}
					
				}
			}
			if(s.contains("package")){
				s = s.replace("package", "packages");
			}
			s +="}";
			return s;
	    }
		public static Map<String, Object> JSONToHashMap(Object param) {
			JSONObject params = JSONObject.parseObject(param.toString());
			Map<String, Object> map = new HashMap<>();
			Set<String> keys = params.keySet();
			if (keys == null || keys.size() == 0) {
				return map;
			}
			Iterator<String> it = keys.iterator();
			while (it.hasNext()) {
				String key = it.next();
				Object value = params.get(key);
				if (value instanceof JSONObject) {
					value = JSONToHashMap(value);
				}
				if (value instanceof JSONArray) {
					value = JSONArrayToArrayList((JSONArray) value);
				}
				map.put(key, value+"");
			}
			return map;
		}
		private static Object JSONArrayToArrayList(JSONArray params) {
			List<Object> list = new ArrayList<>();
			for (int i = 0; i < params.size(); i++) {
				Object item = params.get(i);
				if (item instanceof JSONObject) {
					item = JSONToHashMap((JSONObject) item);
				}
				if (item instanceof JSONArray) {
					item = JSONArrayToArrayList((JSONArray) item);
				}
				list.add(item);
			}
			return list;

		}
		public static <T> Map<String,Object> getClassProNotNull(T t) throws IllegalArgumentException, IllegalAccessException{
			Map<String,Object> treeMap = new TreeMap<String,Object>();
			if(null==t){
				return null;
			}
			Class clazz = t.getClass();
			Field[] fs = clazz.getDeclaredFields();
			for(int i = 0;i<fs.length;i++){
				Field f = fs[i];
				f.setAccessible(true);
				String name = f.getName();
				String val = String.valueOf(f.get(t));
				if(!StringUtil.isEmpty(val)&&!"null".equals(val)&&!"sign".equals(name)){
					treeMap.put(name,val.trim());
				}
			}
			return treeMap;
		}
}
